package poc.bcsoi.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;


public class DateUtils {

	public static Date parseTimestamp(String timestamp) {
		Calendar cal = Calendar.getInstance();
		XMLGregorianCalendar date2 = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy'T'HH:mm:ss.SSS", Locale.US);
			Date d = sdf.parse(timestamp);
			
			GregorianCalendar c = new GregorianCalendar();
			c.setTime(d);
			date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);

			cal.setTime(d);			
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (DatatypeConfigurationException e) {
			e.printStackTrace();
		}
		
		return date2.toGregorianCalendar().getTime();
	}

	
	public static GregorianCalendar convertDateString(String dateString) {
		dateString = dateString.replaceAll("\\-0([0-9]){1}\\:00", "-0$100");
		GregorianCalendar calender = new GregorianCalendar();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
			Date date = sdf.parse(dateString);
			calender.setTime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return calender;
	}
	
	
	public static Date convertStringToDate(String dateString) {	  		
		String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
		DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);		
		DateTime dateTime = dtf.parseDateTime(dateString);
		dateTime.withZone(DateTimeZone.forID("New York"));
		
		System.out.println("\nDATE CONVERTED");
		System.out.println("DatTime.toString():\t" + dateTime.toString());
		System.out.println("DateTime.toDate():\t" + dateTime.withZone(DateTimeZone.UTC).toDate());

		return dateTime.toDate();
	}
	
	
	public static Calendar convertStringToCalendar(String dateString) {	    
		String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
		DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);		
		DateTime dateTime = dtf.parseDateTime(dateString);
		dateTime.withZone(DateTimeZone.forID("America/New_York"));
		
//		System.out.println("\nDATE CONVERTED");
//		System.out.println("DatTime.toString():\t" + dateTime.toString());
//		System.out.println("DateTime.toCalendar():\t" + dateTime.withZone(DateTimeZone.UTC).toCalendar(Locale.US));
		
		return dateTime.toCalendar(Locale.US);
	}
	
	
	public static GregorianCalendar convertStringToGregorianCalendar(String dateString) {	    
		String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
		DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);		
		DateTime dateTime = dtf.parseDateTime(dateString);
		dateTime.withZone(DateTimeZone.forID("America/New_York"));
		
		System.out.println("\nDATE CONVERTED");
		System.out.println("DatTime.toString():\t" + dateTime.toString());
		System.out.println("DateTime.toGregorianCal():\t" + dateTime.withZone(DateTimeZone.UTC).toGregorianCalendar().toString());
		
		return dateTime.toGregorianCalendar();
	}
		
}
